using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using WIT.DAQ.Core.Models;

namespace WIT.DAQ.Core.Forms;

/// <summary>
///
/// </summary>
internal class TaskWritebackEditForm : BaseForm
{
	/// <summary>
	///
	/// </summary>
	/// <param name="writeback"></param>
	internal delegate void SaveTaskWritebackHandler(TaskWriteback writeback);

	/// <summary>
	/// 当前操作的站点
	/// </summary>
	private readonly Station STATION;

	/// <summary>
	/// 实例化的目的
	/// </summary>
	internal readonly EnumFormPurpose PURPOSE;

	/// <summary>
	/// 当次编辑的回写动作
	/// </summary>
	internal readonly TaskWriteback WRITEBACK;

	private const string TITLE_ADD = "添加回写动作";

	private const string TITLE_EDIT = "编辑回写动作";

	/// <summary>
	/// Required designer variable.
	/// </summary>
	private IContainer components = null;

	private Label label1;

	private TextBox txtContent;

	private ComboBox cmbDAQItems;

	private ComboBox cmbContentSource;

	private Label label2;

	private Label label3;

	private Button btnConfirm;

	private Button btnCancel;

	private Label lblNotice;

	/// <summary>
	///
	/// </summary>
	internal event SaveTaskWritebackHandler SaveTaskWriteback;

	/// <summary>
	///
	/// </summary>
	/// <param name="station"></param>
	/// <param name="purpose"></param>
	/// <param name="writeback"></param>
	internal TaskWritebackEditForm(Station station, EnumFormPurpose purpose, TaskWriteback writeback = null)
	{
		InitializeComponent();
		InitDAQItems(station.Id);
		InitContentSources();
		STATION = station;
		PURPOSE = purpose;
		WRITEBACK = writeback;
		switch (PURPOSE)
		{
		case EnumFormPurpose.Add:
			Text = "添加回写动作";
			break;
		case EnumFormPurpose.Edit:
			Text = "编辑回写动作";
			cmbDAQItems.SelectedValue = WRITEBACK.DAQItemId;
			cmbContentSource.SelectedValue = WRITEBACK.ContentSource;
			txtContent.Text = WRITEBACK.Content.ToString();
			break;
		}
	}

	private void TaskWritebackEditForm_Load(object sender, EventArgs e)
	{
		txtContent.Select();
	}

	/// <summary>
	/// 加载采集项
	/// </summary>
	private void InitDAQItems(Guid stationId)
	{
		List<DAQItem> dAQItems = Common.GetDAQItems(stationId);
		cmbDAQItems.DisplayMember = "ShowName";
		cmbDAQItems.ValueMember = "Id";
		cmbDAQItems.DataSource = dAQItems;
	}

	/// <summary>
	/// 加载内容来源
	/// </summary>
	private void InitContentSources()
	{
		cmbContentSource.DisplayMember = "Name";
		cmbContentSource.ValueMember = "Value";
		cmbContentSource.DataSource = TaskWriteback.ContentSources;
	}

	/// <summary>
	/// 内容来源选择项改变时，作出相应的提示
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void cmbContentSource_SelectedIndexChanged(object sender, EventArgs e)
	{
		string text = cmbContentSource.SelectedValue.ToString();
		string text2 = text;
		if (!(text2 == "Cosnt"))
		{
			if (text2 == "Variable")
			{
				lblNotice.Text = "填入对应的变量名";
			}
			else
			{
				lblNotice.Text = string.Empty;
			}
		}
		else
		{
			lblNotice.Text = "回写常量";
		}
	}

	/// <summary>
	/// 确认
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnConfirm_Click(object sender, EventArgs e)
	{
		Guid guid = new Guid(cmbDAQItems.SelectedValue.ToString());
		TaskWriteback taskWriteback = null;
		switch (PURPOSE)
		{
		case EnumFormPurpose.Add:
			taskWriteback = new TaskWriteback
			{
				Id = Guid.NewGuid(),
				StationId = STATION.Id,
				DAQItemId = guid,
				DAQItem = Common.GetDAQItem(STATION.Id, guid),
				ContentSource = cmbContentSource.SelectedValue.ToString(),
				Content = txtContent.Text.Trim()
			};
			break;
		case EnumFormPurpose.Edit:
			taskWriteback = WRITEBACK;
			taskWriteback.DAQItemId = guid;
			taskWriteback.DAQItem = Common.GetDAQItem(STATION.Id, guid);
			taskWriteback.ContentSource = cmbContentSource.SelectedValue.ToString();
			taskWriteback.Content = txtContent.Text.Trim();
			break;
		}
		this.SaveTaskWriteback(taskWriteback);
		Close();
	}

	/// <summary>
	/// 关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	/// <summary>
	/// ESC关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void TaskWritebackEditForm_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Escape)
		{
			btnCancel_Click(null, null);
		}
	}

	/// <summary>
	/// Clean up any resources being used.
	/// </summary>
	/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{
		this.label1 = new System.Windows.Forms.Label();
		this.txtContent = new System.Windows.Forms.TextBox();
		this.cmbDAQItems = new System.Windows.Forms.ComboBox();
		this.cmbContentSource = new System.Windows.Forms.ComboBox();
		this.label2 = new System.Windows.Forms.Label();
		this.label3 = new System.Windows.Forms.Label();
		this.btnConfirm = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.lblNotice = new System.Windows.Forms.Label();
		base.SuspendLayout();
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(27, 27);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(53, 12);
		this.label1.TabIndex = 0;
		this.label1.Text = "采集项：";
		this.txtContent.Location = new System.Drawing.Point(86, 50);
		this.txtContent.Name = "txtContent";
		this.txtContent.Size = new System.Drawing.Size(150, 21);
		this.txtContent.TabIndex = 1;
		this.cmbDAQItems.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbDAQItems.FormattingEnabled = true;
		this.cmbDAQItems.Location = new System.Drawing.Point(86, 24);
		this.cmbDAQItems.Name = "cmbDAQItems";
		this.cmbDAQItems.Size = new System.Drawing.Size(300, 20);
		this.cmbDAQItems.TabIndex = 0;
		this.cmbContentSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbContentSource.FormattingEnabled = true;
		this.cmbContentSource.Location = new System.Drawing.Point(86, 77);
		this.cmbContentSource.Name = "cmbContentSource";
		this.cmbContentSource.Size = new System.Drawing.Size(150, 20);
		this.cmbContentSource.TabIndex = 2;
		this.cmbContentSource.SelectedIndexChanged += new System.EventHandler(cmbContentSource_SelectedIndexChanged);
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(15, 80);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(65, 12);
		this.label2.TabIndex = 0;
		this.label2.Text = "内容来源：";
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(15, 53);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(65, 12);
		this.label3.TabIndex = 0;
		this.label3.Text = "回写内容：";
		this.btnConfirm.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnConfirm.Location = new System.Drawing.Point(311, 156);
		this.btnConfirm.Name = "btnConfirm";
		this.btnConfirm.Size = new System.Drawing.Size(75, 23);
		this.btnConfirm.TabIndex = 3;
		this.btnConfirm.Text = "确认(&S)";
		this.btnConfirm.UseVisualStyleBackColor = true;
		this.btnConfirm.Click += new System.EventHandler(btnConfirm_Click);
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.Location = new System.Drawing.Point(392, 156);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 4;
		this.btnCancel.Text = "取消(&C)";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.lblNotice.AutoSize = true;
		this.lblNotice.Font = new System.Drawing.Font("宋体", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		this.lblNotice.ForeColor = System.Drawing.SystemColors.ControlDark;
		this.lblNotice.Location = new System.Drawing.Point(242, 55);
		this.lblNotice.Name = "lblNotice";
		this.lblNotice.Size = new System.Drawing.Size(41, 12);
		this.lblNotice.TabIndex = 5;
		this.lblNotice.Text = "notice";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(479, 191);
		base.Controls.Add(this.lblNotice);
		base.Controls.Add(this.btnConfirm);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.cmbContentSource);
		base.Controls.Add(this.cmbDAQItems);
		base.Controls.Add(this.txtContent);
		base.Controls.Add(this.label3);
		base.Controls.Add(this.label2);
		base.Controls.Add(this.label1);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.MaximizeBox = false;
		base.Name = "TaskWritebackEditForm";
		this.Text = "TaskConditionEditForm";
		base.Load += new System.EventHandler(TaskWritebackEditForm_Load);
		base.KeyDown += new System.Windows.Forms.KeyEventHandler(TaskWritebackEditForm_KeyDown);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
